home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / src / event.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-02  |  47.3 KB  |  1,872 lines

  1. /* event.c- event loop and handling
  2.  * 
  3.  *  Window Maker window manager
  4.  * 
  5.  *  Copyright (c) 1997, 1998 Alfredo K. Kojima
  6.  * 
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; if not, write to the Free Software
  19.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
  20.  *  USA.
  21.  */
  22.  
  23.  
  24. #include "wconfig.h"
  25.  
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <string.h>
  31.  
  32. #include <nana.h>
  33.  
  34.  
  35. #include <X11/Xlib.h>
  36. #include <X11/Xutil.h>
  37. #ifdef SHAPE
  38. # include <X11/extensions/shape.h>
  39. #endif
  40. #ifdef XDND
  41. #include "xdnd.h"
  42. #endif
  43.  
  44. #ifdef KEEP_XKB_LOCK_STATUS     
  45. #include <X11/XKBlib.h>         
  46. #endif /* KEEP_XKB_LOCK_STATUS */
  47.  
  48. #include "WindowMaker.h"
  49. #include "window.h"
  50. #include "actions.h"
  51. #include "client.h"
  52. #include "funcs.h"
  53. #include "keybind.h"
  54. #include "application.h"
  55. #include "stacking.h"
  56. #include "defaults.h"
  57. #include "workspace.h"
  58. #include "dock.h"
  59. #include "framewin.h"
  60. #include "properties.h"
  61. #include "balloon.h"
  62.  
  63. #ifdef GNOME_STUFF
  64. # include "gnome.h"
  65. #endif
  66. #ifdef KWM_HINTS
  67. # include "kwm.h"
  68. #endif
  69.  
  70. /******** Global Variables **********/
  71. extern XContext wWinContext;
  72.  
  73. extern Cursor wCursor[WCUR_LAST];
  74.  
  75. extern WShortKey wKeyBindings[WKBD_LAST];
  76. extern int wScreenCount;
  77. extern Time LastTimestamp;
  78. extern Time LastFocusChange;
  79.  
  80. extern WPreferences wPreferences;
  81.  
  82. #define MOD_MASK wPreferences.modifier_mask
  83.  
  84. extern Atom _XA_WM_COLORMAP_NOTIFY;
  85.  
  86. extern Atom _XA_WM_CHANGE_STATE;
  87. extern Atom _XA_WM_DELETE_WINDOW;
  88. extern Atom _XA_GNUSTEP_WM_ATTR;
  89. extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
  90. extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
  91. extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
  92. extern Atom _XA_WINDOWMAKER_COMMAND;
  93.  
  94. #ifdef OFFIX_DND
  95. extern Atom _XA_DND_PROTOCOL;
  96. #endif
  97.  
  98.  
  99. #ifdef SHAPE
  100. extern Bool wShapeSupported;
  101. extern int wShapeEventBase;
  102. #endif
  103.  
  104. #ifdef KEEP_XKB_LOCK_STATUS     
  105. extern int wXkbEventBase;
  106. #endif
  107.  
  108. /* special flags */
  109. extern char WDelayedActionSet;
  110.  
  111.  
  112. /************ Local stuff ***********/
  113.  
  114.  
  115. static void saveTimestamp(XEvent *event);
  116. static void handleColormapNotify();
  117. static void handleMapNotify(), handleUnmapNotify();
  118. static void handleButtonPress(), handleExpose();
  119. static void handleDestroyNotify();
  120. static void handleConfigureRequest();
  121. static void handleMapRequest();
  122. static void handlePropertyNotify();
  123. static void handleEnterNotify();
  124. static void handleLeaveNotify();
  125. static void handleExtensions();
  126. static void handleClientMessage();
  127. static void handleKeyPress();
  128. static void handleFocusIn();
  129. static void handleMotionNotify();
  130. static void handleVisibilityNotify();
  131.  
  132.  
  133. #ifdef SHAPE
  134. static void handleShapeNotify();
  135. #endif
  136.  
  137. /* called from the signal handler */
  138. void NotifyDeadProcess(pid_t pid, unsigned char status);
  139.  
  140. /* real dead process handler */
  141. static void handleDeadProcess(void *foo);
  142.  
  143.  
  144. typedef struct DeadProcesses {
  145.     pid_t         pid;
  146.     unsigned char     exit_status;
  147. } DeadProcesses;
  148.  
  149. /* stack of dead processes */
  150. static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
  151. static int deadProcessPtr=0;
  152.  
  153.  
  154. typedef struct DeathHandler {
  155.     WDeathHandler    *callback;
  156.     pid_t         pid;
  157.     void         *client_data;
  158. } DeathHandler;
  159.  
  160. static WMBag *deathHandlers=NULL;
  161.  
  162.  
  163.  
  164. WMagicNumber
  165. wAddDeathHandler(pid_t pid, WDeathHandler *callback, void *cdata)
  166. {
  167.     DeathHandler *handler;
  168.     
  169.     handler = malloc(sizeof(DeathHandler));
  170.     if (!handler)
  171.       return 0;
  172.  
  173.     handler->pid = pid;
  174.     handler->callback = callback;
  175.     handler->client_data = cdata;
  176.  
  177.     if (!deathHandlers)
  178.     deathHandlers = WMCreateBag(8);
  179.  
  180.     WMPutInBag(deathHandlers, handler);
  181.  
  182.     return handler;
  183. }
  184.  
  185.  
  186.  
  187. void 
  188. wDeleteDeathHandler(WMagicNumber id)
  189. {
  190.     DeathHandler *handler=(DeathHandler*)id;
  191.  
  192.     if (!handler || !deathHandlers)
  193.       return;
  194.  
  195.     WMRemoveFromBag(deathHandlers, handler);
  196.  
  197.     free(handler);
  198. }
  199.  
  200.  
  201. void
  202. DispatchEvent(XEvent *event)
  203. {
  204.     if (deathHandlers)
  205.     handleDeadProcess(NULL);
  206.  
  207.     if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
  208.     WCHANGE_STATE(WSTATE_EXITING);
  209.     /* received SIGTERM */
  210.     /* 
  211.      * WMHandleEvent() can't be called from anything
  212.      * executed inside here, or we can get in a infinite
  213.      * recursive loop.
  214.      */
  215.     Shutdown(WSExitMode);
  216.  
  217.     } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
  218.     WCHANGE_STATE(WSTATE_RESTARTING);
  219.  
  220.     Shutdown(WSRestartPreparationMode);
  221.     /* received SIGHUP */
  222.     Restart(NULL, True);
  223.     }
  224.  
  225.     /* for the case that all that is wanted to be dispatched is
  226.      * the stuff above */
  227.     if (!event)
  228.     return;
  229.  
  230.     saveTimestamp(event);
  231.     switch (event->type) {
  232.      case MapRequest:
  233.     handleMapRequest(event);
  234.     break;
  235.  
  236.      case KeyPress:
  237.     handleKeyPress(event);
  238.     break;
  239.  
  240.      case MotionNotify:
  241.          handleMotionNotify(event);
  242.          break;
  243.  
  244.      case ConfigureRequest:
  245.     handleConfigureRequest(event);
  246.     break;
  247.  
  248.      case DestroyNotify:
  249.     handleDestroyNotify(event);
  250.     break;
  251.     
  252.      case MapNotify:
  253.     handleMapNotify(event);
  254.     break;
  255.     
  256.      case UnmapNotify:
  257.     handleUnmapNotify(event);
  258.     break;
  259.  
  260.      case ButtonPress:
  261.     handleButtonPress(event);
  262.     break;
  263.     
  264.      case Expose:
  265.     handleExpose(event);
  266.     break;
  267.     
  268.      case PropertyNotify:
  269.     handlePropertyNotify(event);
  270.     break;
  271.  
  272.      case EnterNotify:
  273.     handleEnterNotify(event);
  274.     break;
  275.  
  276.      case LeaveNotify:
  277.         handleLeaveNotify(event);
  278.     break;
  279.  
  280.      case ClientMessage:
  281.     handleClientMessage(event);
  282.     break;
  283.     
  284.      case ColormapNotify:
  285.     handleColormapNotify(event);
  286.     break;
  287.  
  288.      case MappingNotify:
  289.     if (event->xmapping.request == MappingKeyboard 
  290.         || event->xmapping.request == MappingModifier)
  291.       XRefreshKeyboardMapping(&event->xmapping);
  292.     break;
  293.     
  294.      case FocusIn:
  295.     handleFocusIn(event);
  296.     break;
  297.  
  298.      case VisibilityNotify:
  299.     handleVisibilityNotify(event);
  300.     break;
  301.      default:
  302.     handleExtensions(event);
  303.     break;
  304.     }
  305. }
  306.  
  307.  
  308. /*
  309.  *----------------------------------------------------------------------
  310.  * EventLoop-
  311.  *     Processes X and internal events indefinitely.
  312.  * 
  313.  * Returns:
  314.  *     Never returns
  315.  * 
  316.  * Side effects:
  317.  *     The LastTimestamp global variable is updated.
  318.  *---------------------------------------------------------------------- 
  319.  */
  320. void
  321. EventLoop()
  322. {
  323.     XEvent event;
  324.    
  325.     for(;;) {
  326.     WMNextEvent(dpy, &event);
  327.     WMHandleEvent(&event);
  328.     }
  329. }
  330.  
  331.  
  332.  
  333. Bool 
  334. IsDoubleClick(WScreen *scr, XEvent *event)
  335. {
  336.     if ((scr->last_click_time>0) && 
  337.     (event->xbutton.time-scr->last_click_time<=wPreferences.dblclick_time)
  338.     && (event->xbutton.button == scr->last_click_button)
  339.     && (event->xbutton.window == scr->last_click_window)) {
  340.  
  341.     scr->flags.next_click_is_not_double = 1;
  342.     scr->last_click_time = 0;
  343.     scr->last_click_window = event->xbutton.window;
  344.  
  345.     return True;
  346.     }
  347.     return False;
  348. }
  349.  
  350.  
  351. void
  352. NotifyDeadProcess(pid_t pid, unsigned char status)
  353. {
  354.     if (deadProcessPtr>=MAX_DEAD_PROCESSES-1) {
  355.     wwarning("stack overflow: too many dead processes");
  356.     return;
  357.     }
  358.     /* stack the process to be handled later,
  359.      * as this is called from the signal handler */
  360.     deadProcesses[deadProcessPtr].pid = pid;
  361.     deadProcesses[deadProcessPtr].exit_status = status;
  362.     deadProcessPtr++;
  363. }
  364.  
  365.  
  366. static void
  367. handleDeadProcess(void *foo)
  368. {
  369.     DeathHandler *tmp;
  370.     int i;
  371.  
  372.     for (i=0; i<deadProcessPtr; i++) {
  373.     wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
  374.     }
  375.  
  376.     if (!deathHandlers) {
  377.     deadProcessPtr=0;
  378.     return;
  379.     }
  380.     
  381.     /* get the pids on the queue and call handlers */
  382.     while (deadProcessPtr>0) {
  383.     deadProcessPtr--;
  384.  
  385.     for (i = WMGetBagItemCount(deathHandlers)-1; i >= 0; i--) {
  386.         tmp = WMGetFromBag(deathHandlers, i);
  387.         if (!tmp)
  388.         continue;
  389.  
  390.         if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
  391.         (*tmp->callback)(tmp->pid, 
  392.                  deadProcesses[deadProcessPtr].exit_status,
  393.                  tmp->client_data);
  394.         wDeleteDeathHandler(tmp);
  395.         }
  396.     }
  397.     }
  398. }
  399.  
  400.  
  401. static void
  402. saveTimestamp(XEvent *event)
  403. {
  404.     LastTimestamp = CurrentTime;
  405.  
  406.     switch (event->type) {
  407.      case ButtonRelease:
  408.      case ButtonPress:
  409.     LastTimestamp = event->xbutton.time;
  410.     break;
  411.      case KeyPress:
  412.      case KeyRelease:
  413.     LastTimestamp = event->xkey.time;
  414.     break;
  415.      case MotionNotify:
  416.     LastTimestamp = event->xmotion.time;
  417.     break;
  418.      case PropertyNotify:
  419.     LastTimestamp = event->xproperty.time;
  420.     break;
  421.      case EnterNotify:
  422.      case LeaveNotify:
  423.     LastTimestamp = event->xcrossing.time;
  424.     break;
  425.      case SelectionClear:
  426.     LastTimestamp = event->xselectionclear.time;
  427.     break;
  428.      case SelectionRequest:
  429.     LastTimestamp = event->xselectionrequest.time;
  430.     break;
  431.      case SelectionNotify:
  432.     LastTimestamp = event->xselection.time;
  433. #ifdef XDND
  434.     wXDNDProcessSelection(event);
  435. #endif
  436.     break;
  437.     }
  438. }
  439.  
  440.  
  441. static void
  442. handleExtensions(XEvent *event)
  443. {
  444. #ifdef KEEP_XKB_LOCK_STATUS   
  445.     XkbEvent *xkbevent;
  446.     xkbevent = (XkbEvent *)event;
  447. #endif /*KEEP_XKB_LOCK_STATUS*/
  448. #ifdef SHAPE
  449.     if (wShapeSupported && event->type == (wShapeEventBase+ShapeNotify)) {
  450.     handleShapeNotify(event);
  451.     }
  452. #endif    
  453. #ifdef KEEP_XKB_LOCK_STATUS   
  454.     if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)){
  455.         handleXkbIndicatorStateNotify(event);
  456.     }
  457. #endif /*KEEP_XKB_LOCK_STATUS*/
  458. }
  459.  
  460.  
  461. static void 
  462. handleMapRequest(XEvent *ev)
  463. {
  464.     WWindow *wwin;
  465.     WScreen *scr = NULL;
  466.     Window window = ev->xmaprequest.window;
  467.  
  468. #ifdef DEBUG
  469.     L("got map request for %x\n", (unsigned)window);
  470. #endif
  471.     if ((wwin = wWindowFor(window))) {
  472.     if (wwin->flags.shaded) {
  473.         wUnshadeWindow(wwin);
  474.     }
  475.     /* deiconify window */
  476.     if (wwin->flags.miniaturized) {
  477.         wDeiconifyWindow(wwin);
  478.     } else if (wwin->flags.hidden) {
  479.             WApplication *wapp = wApplicationOf(wwin->main_window);
  480.             /* go to the last workspace that the user worked on the app */
  481. #ifndef REDUCE_APPICONS
  482.             /* This severely breaks REDUCE_APPICONS.  last_workspace is a neat
  483.              * concept but it needs to be reworked to handle REDUCE_APPICONS -cls
  484.              */
  485.             if (wapp) {
  486.                 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
  487.             }
  488. #endif
  489.         wUnhideApplication(wapp, False, False);
  490.     }
  491.     return;
  492.     }
  493.  
  494.     scr = wScreenForRootWindow(ev->xmaprequest.parent);
  495.  
  496.     wwin = wManageWindow(scr, window);
  497.  
  498.     /* 
  499.      * This is to let the Dock know that the application it launched
  500.      * has already been mapped (eg: it has finished launching). 
  501.      * It is not necessary for normally docked apps, but is needed for
  502.      * apps that were forcedly docked (like with dockit).
  503.      */
  504.     if (scr->last_dock) {
  505.     if (wwin && wwin->main_window!=None && wwin->main_window!=window)
  506.       wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
  507.     else
  508.       wDockTrackWindowLaunch(scr->last_dock, window);
  509.     }
  510.  
  511.     if (wwin) {
  512.     wClientSetState(wwin, NormalState, None);
  513.     if (wwin->flags.maximized) {
  514.         wMaximizeWindow(wwin, wwin->flags.maximized);
  515.     }
  516.     if (wwin->flags.shaded) {
  517.         wwin->flags.shaded = 0;
  518.         wwin->flags.skip_next_animation = 1;
  519.         wShadeWindow(wwin);
  520.     }
  521.     if (wwin->flags.miniaturized) {
  522.         wwin->flags.miniaturized = 0;
  523.         wwin->flags.skip_next_animation = 1;
  524.         wIconifyWindow(wwin);
  525.     }
  526.     if (wwin->flags.hidden) {
  527.         WApplication *wapp = wApplicationOf(wwin->main_window);
  528.  
  529.         wwin->flags.hidden = 0;
  530.         wwin->flags.skip_next_animation = 1;
  531.         if (wapp) {
  532.         wHideApplication(wapp);
  533.         }
  534.     }
  535.     }
  536. }
  537.  
  538.  
  539. static void
  540. handleDestroyNotify(XEvent *event)
  541. {
  542.     WWindow *wwin;
  543.     WApplication *app;
  544.     Window window = event->xdestroywindow.window;
  545.  
  546. #ifdef DEBUG
  547.     L("got destroy notify");
  548. #endif
  549.     wwin = wWindowFor(window);
  550.     if (wwin) {
  551.     wUnmanageWindow(wwin, False, True);
  552.     }
  553.  
  554.     app = wApplicationOf(window);
  555.     if (app) {
  556.     if (window == app->main_window) {
  557.         app->refcount = 0;
  558.         wwin = app->main_window_desc->screen_ptr->focused_window;
  559.         while (wwin) {
  560.         if (wwin->main_window == window) {
  561.             wwin->main_window = None;
  562.         }
  563.         wwin = wwin->prev;
  564.         }
  565.     }
  566.     wApplicationDestroy(app);
  567.     }
  568.     
  569. #ifdef KWM_HINTS
  570.     wKWMCheckDestroy(&event->xdestroywindow);
  571. #endif
  572. }
  573.  
  574.  
  575.  
  576. static void 
  577. handleExpose(XEvent *event)
  578. {
  579.     WObjDescriptor *desc;
  580.     XEvent ev;
  581.  
  582. #ifdef DEBUG
  583.     L("got expose");
  584. #endif
  585.     while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev));
  586.  
  587.     if (XFindContext(dpy, event->xexpose.window, wWinContext, 
  588.              (XPointer *)&desc)==XCNOENT) {
  589.     return;
  590.     }
  591.     
  592.     if (desc->handle_expose) {
  593.     (*desc->handle_expose)(desc, event);
  594.     }
  595. }
  596.  
  597. /* bindable */
  598. static void
  599. handleButtonPress(XEvent *event)
  600. {
  601.     WObjDescriptor *desc;
  602.     WScreen *scr;
  603. #ifdef DEBUG
  604.     L("got button press");
  605. #endif
  606.     scr = wScreenForRootWindow(event->xbutton.root);
  607.     
  608. #ifdef BALLOON_TEXT
  609.     wBalloonHide(scr);
  610. #endif
  611.  
  612.     
  613. #ifndef LITE
  614.     if (event->xbutton.window==scr->root_win) {    
  615.     if (event->xbutton.button==wPreferences.menu_button) {
  616.         OpenRootMenu(scr, event->xbutton.x_root,
  617.                          event->xbutton.y_root, False);
  618.         /* ugly hack */
  619.         if (scr->root_menu) {
  620.         if (scr->root_menu->brother->flags.mapped)
  621.           event->xbutton.window = scr->root_menu->brother->frame->core->window;
  622.         else
  623.           event->xbutton.window = scr->root_menu->frame->core->window;
  624.         }
  625.     } else if (event->xbutton.button==wPreferences.windowl_button) {
  626.         OpenSwitchMenu(scr, event->xbutton.x_root,
  627.                event->xbutton.y_root, False);
  628.         if (scr->switch_menu) {
  629.         if (scr->switch_menu->brother->flags.mapped)
  630.           event->xbutton.window = scr->switch_menu->brother->frame->core->window;
  631.         else
  632.           event->xbutton.window = scr->switch_menu->frame->core->window;
  633.         }
  634.     } else if (event->xbutton.button==wPreferences.select_button) {
  635.         wUnselectWindows(scr);
  636.         wSelectWindows(scr, event);
  637.     }
  638. #ifdef MOUSE_WS_SWITCH
  639.     else if (event->xbutton.button==Button5) {
  640.  
  641.         wWorkspaceRelativeChange(scr, -1);
  642.  
  643.     } else if (event->xbutton.button==Button4) {
  644.  
  645.         wWorkspaceRelativeChange(scr, 1);
  646.  
  647.     }
  648. #endif /* MOUSE_WS_SWITCH */
  649. #ifdef GNOME_STUFF
  650.     else if (wGNOMEProxyizeButtonEvent(scr, event))
  651.         return;
  652. #endif
  653.     }
  654. #endif /* !LITE */
  655.  
  656.     desc = NULL;
  657.     if (XFindContext(dpy, event->xbutton.subwindow, wWinContext,
  658.              (XPointer *)&desc)==XCNOENT) {
  659.     if (XFindContext(dpy, event->xbutton.window, wWinContext,
  660.              (XPointer *)&desc)==XCNOENT) {
  661.         return;
  662.     }
  663.     }
  664.  
  665.     if (desc->parent_type == WCLASS_WINDOW) {
  666.     XSync(dpy, 0);
  667.     
  668.     if (event->xbutton.state & MOD_MASK) {
  669.         XAllowEvents(dpy, AsyncPointer, CurrentTime);
  670.     }
  671.      
  672.     if (wPreferences.focus_mode == WKF_CLICK) {
  673.         if (wPreferences.ignore_focus_click) {
  674.         XAllowEvents(dpy, AsyncPointer, CurrentTime);
  675.         }
  676.         XAllowEvents(dpy, ReplayPointer, CurrentTime);
  677.     }
  678.     XSync(dpy, 0);
  679.     } else if (desc->parent_type == WCLASS_APPICON
  680.            || desc->parent_type == WCLASS_MINIWINDOW
  681.            || desc->parent_type == WCLASS_DOCK_ICON) {
  682.     if (event->xbutton.state & MOD_MASK) {
  683.         XSync(dpy, 0);
  684.         XAllowEvents(dpy, AsyncPointer, CurrentTime);
  685.         XSync(dpy, 0);
  686.     }
  687.     }
  688.  
  689.     if (desc->handle_mousedown!=NULL) {
  690.     (*desc->handle_mousedown)(desc, event);
  691.     }
  692.  
  693.     /* save double-click information */
  694.     if (scr->flags.next_click_is_not_double) {
  695.     scr->flags.next_click_is_not_double = 0;
  696.     } else {
  697.     scr->last_click_time = event->xbutton.time;
  698.     scr->last_click_button = event->xbutton.button;
  699.     scr->last_click_window = event->xbutton.window;
  700.     }
  701. }
  702.  
  703.  
  704. static void
  705. handleMapNotify(XEvent *event)
  706. {
  707.     WWindow *wwin;
  708. #ifdef DEBUG
  709.     L("got map");
  710. #endif
  711.     wwin = wWindowFor(event->xmap.event);
  712.     if (wwin && wwin->client_win == event->xmap.event) {
  713.     if (wwin->flags.miniaturized) {
  714.         wDeiconifyWindow(wwin);
  715.     } else {
  716.         XGrabServer(dpy);
  717.         wWindowMap(wwin);
  718.         wClientSetState(wwin, NormalState, None);
  719.         XUngrabServer(dpy);
  720.     }
  721.     }
  722. }
  723.  
  724.  
  725. static void
  726. handleUnmapNotify(XEvent *event)
  727. {
  728.     WWindow *wwin;
  729.     XEvent ev;
  730.     Bool withdraw = False;
  731. #ifdef DEBUG
  732.     L("got unmap");
  733. #endif
  734.     /* only process windows with StructureNotify selected 
  735.      * (ignore SubstructureNotify) */
  736.     wwin = wWindowFor(event->xunmap.window);
  737.     if (!wwin)
  738.     return;
  739.  
  740.     /* whether the event is a Withdrawal request */
  741.     if (event->xunmap.event == wwin->screen_ptr->root_win
  742.     && event->xunmap.send_event)
  743.     withdraw = True;
  744.  
  745.     if (wwin->client_win != event->xunmap.event && !withdraw)
  746.     return;
  747.  
  748.     if (!wwin->flags.mapped && !withdraw
  749.     && wwin->frame->workspace == wwin->screen_ptr->current_workspace
  750.     && !wwin->flags.miniaturized && !wwin->flags.hidden)
  751.     return;
  752.  
  753.     XGrabServer(dpy);
  754.     XUnmapWindow(dpy, wwin->frame->core->window);
  755.     wwin->flags.mapped = 0;
  756.     XSync(dpy, 0);
  757.     /* check if the window was destroyed */
  758.     if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify,&ev)) {
  759.     DispatchEvent(&ev);
  760.     } else {
  761.     Bool reparented = False;
  762.  
  763.     if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
  764.         reparented = True;
  765.  
  766.     /* withdraw window */
  767.     wwin->flags.mapped = 0;
  768.     if (!reparented)
  769.         wClientSetState(wwin, WithdrawnState, None);
  770.     
  771.     /* if the window was reparented, do not reparent it back to the
  772.      * root window */
  773.     wUnmanageWindow(wwin, !reparented, False);
  774.     }
  775.     XUngrabServer(dpy);
  776. }
  777.  
  778.  
  779. static void
  780. handleConfigureRequest(XEvent *event)
  781. {
  782.     WWindow *wwin;
  783. #ifdef DEBUG
  784.     L("got configure request");
  785. #endif
  786.     if (!(wwin=wWindowFor(event->xconfigurerequest.window))) {
  787.     /*
  788.      * Configure request for unmapped window
  789.      */
  790.     wClientConfigure(NULL, &(event->xconfigurerequest));
  791.     } else {
  792.     wClientConfigure(wwin, &(event->xconfigurerequest));
  793.     }
  794. }
  795.  
  796.  
  797. static void
  798. handlePropertyNotify(XEvent *event)
  799. {
  800.     WWindow *wwin;
  801.     WApplication *wapp;
  802.     Window jr;
  803.     int ji;
  804.     unsigned int ju;
  805.     WScreen *scr;
  806. #ifdef DEBUG
  807.     L("got property notify");
  808. #endif
  809.     if ((wwin=wWindowFor(event->xproperty.window))) {
  810.     if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji,
  811.               &ju, &ju, &ju, &ju)) {
  812.         return;
  813.     }
  814.     wClientCheckProperty(wwin, &event->xproperty);
  815.     }
  816.     wapp = wApplicationOf(event->xproperty.window);
  817.     if (wapp) {
  818.     wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
  819.     }
  820.     
  821.     scr = wScreenForWindow(event->xproperty.window);
  822.     if (scr && scr->root_win == event->xproperty.window) {
  823. #ifdef KWM_HINTS
  824.     wKWMCheckRootHintChange(scr, &event->xproperty);
  825. #endif
  826.     }
  827. }
  828.  
  829.  
  830. static void
  831. handleClientMessage(XEvent *event)
  832. {
  833.     WWindow *wwin;
  834.     WObjDescriptor *desc;
  835. #ifdef DEBUG
  836.     L("got client message");
  837. #endif
  838.     /* handle transition from Normal to Iconic state */
  839.     if (event->xclient.message_type == _XA_WM_CHANGE_STATE
  840.     && event->xclient.format == 32 
  841.     && event->xclient.data.l[0] == IconicState) {
  842.     
  843.     wwin = wWindowFor(event->xclient.window);
  844.     if (!wwin) return;
  845.     if (!wwin->flags.miniaturized)
  846.         wIconifyWindow(wwin);
  847.     } else if (event->xclient.message_type == _XA_WM_COLORMAP_NOTIFY
  848.            && event->xclient.format == 32) {
  849.     WScreen *scr = wScreenSearchForRootWindow(event->xclient.window);
  850.  
  851.     if (!scr)
  852.         return;
  853.  
  854.     if (event->xclient.data.l[1] == 1) {   /* starting */
  855.         wColormapAllowClientInstallation(scr, True);
  856.     } else {               /* stopping */
  857.         wColormapAllowClientInstallation(scr, False);
  858.     }
  859.     } else if (event->xclient.message_type == _XA_WINDOWMAKER_COMMAND) {
  860.  
  861.     wDefaultsCheckDomains("bla");
  862.  
  863.     } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
  864.     WApplication *wapp;
  865.     int done=0;
  866.     wapp = wApplicationOf(event->xclient.window);
  867.     if (wapp) {
  868.         switch (event->xclient.data.l[0]) {
  869.          case WMFHideOtherApplications:
  870.         wHideOtherApplications(wapp->main_window_desc);
  871.         done = 1;
  872.         break;
  873.  
  874.          case WMFHideApplication:
  875.         wHideApplication(wapp);
  876.         done = 1;
  877.         break;
  878.         }
  879.     }
  880.     if (!done) {
  881.         wwin = wWindowFor(event->xclient.window);
  882.         if (wwin) {
  883.         switch (event->xclient.data.l[0]) {
  884.          case WMFHideOtherApplications:
  885.             wHideOtherApplications(wwin);
  886.             break;
  887.  
  888.          case WMFHideApplication:
  889.             wHideApplication(wApplicationOf(wwin->main_window));
  890.             break;
  891.         }
  892.         }
  893.     }
  894.     } else if (event->xclient.message_type == _XA_GNUSTEP_WM_ATTR) {
  895.     wwin = wWindowFor(event->xclient.window);
  896.     if (!wwin) return;
  897.     switch (event->xclient.data.l[0]) {
  898.      case GSWindowLevelAttr:
  899.        {
  900.          int level = (int)event->xclient.data.l[1];
  901.  
  902.          if (WINDOW_LEVEL(wwin) != level) {
  903.            ChangeStackingLevel(wwin->frame->core, level);
  904.          }
  905.        }
  906.        break;
  907.     }
  908.     } else if (event->xclient.message_type == _XA_GNUSTEP_TITLEBAR_STATE) {
  909.     wwin = wWindowFor(event->xclient.window);
  910.     if (!wwin) return;
  911.     switch (event->xclient.data.l[0]) {
  912.      case WMTitleBarNormal:
  913.         wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
  914.         break;
  915.      case WMTitleBarMain:
  916.         wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
  917.         break;
  918.      case WMTitleBarKey:
  919.         wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
  920.         break;
  921.     }
  922. #ifdef GNOME_STUFF
  923.     } else if (wGNOMEProcessClientMessage(&event->xclient)) {
  924.     /* do nothing */
  925. #endif /* GNOME_STUFF */
  926. #ifdef KWM_HINTS
  927.     } else if (wKWMProcessClientMessage(&event->xclient)) {
  928.     /* do nothing */
  929. #endif /* KWM_HINTS */
  930. #ifdef XDND
  931.     } else if (wXDNDProcessClientMessage(&event->xclient)) {
  932.     /* do nothing */
  933. #endif /* XDND */
  934. #ifdef OFFIX_DND
  935.     } else if (event->xclient.message_type==_XA_DND_PROTOCOL) {
  936.     WScreen *scr = wScreenForWindow(event->xclient.window);
  937.         if (scr && wDockReceiveDNDDrop(scr,event))
  938.         goto redirect_message;
  939. #endif /* OFFIX_DND */
  940.     } else {
  941. #ifdef OFFIX_DND
  942.      redirect_message:
  943. #endif
  944.     /*
  945.      * Non-standard thing, but needed by OffiX DND.
  946.      * For when the icon frame gets a ClientMessage
  947.      * that should have gone to the icon_window.
  948.      */
  949.     if (XFindContext(dpy, event->xbutton.window, wWinContext, 
  950.              (XPointer *)&desc)!=XCNOENT) {
  951.         struct WIcon *icon=NULL;
  952.  
  953.         if (desc->parent_type == WCLASS_MINIWINDOW) {
  954.         icon = (WIcon*)desc->parent;
  955.         } else if (desc->parent_type == WCLASS_DOCK_ICON
  956.              || desc->parent_type == WCLASS_APPICON) {
  957.         icon = ((WAppIcon*)desc->parent)->icon;
  958.         }
  959.         if (icon && (wwin=icon->owner)) {
  960.         if (wwin->client_win!=event->xclient.window) {
  961.             event->xclient.window = wwin->client_win;
  962.             XSendEvent(dpy, wwin->client_win, False, NoEventMask, 
  963.                    event);
  964.         }
  965.         }
  966.     }
  967.     }
  968. }
  969.  
  970.  
  971. static void
  972. raiseWindow(WScreen *scr)
  973. {
  974.     WWindow *wwin;
  975.  
  976.     scr->autoRaiseTimer = NULL;
  977.  
  978.     wwin = wWindowFor(scr->autoRaiseWindow);    
  979.     if (!wwin)
  980.     return;
  981.  
  982.     if (!wwin->flags.destroyed && wwin->flags.focused) {
  983.     wRaiseFrame(wwin->frame->core);
  984.     /* this is needed or a race condition will occur */
  985.     XSync(dpy, False);
  986.     }
  987. }
  988.  
  989.  
  990. static void 
  991. handleEnterNotify(XEvent *event)
  992. {
  993.     WWindow *wwin;
  994.     WObjDescriptor *desc = NULL;
  995.     XEvent ev;
  996.     WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
  997. #ifdef DEBUG
  998.     L("got enter notify");
  999. #endif
  1000.  
  1001. #ifdef VIRTUAL_DESKTOP
  1002.     if (wPreferences.vedge_thickness) {
  1003.         int x,y;
  1004.         if (event->xcrossing.window == scr->virtual_edge_r) {
  1005.             XWarpPointer(dpy, None, scr->root_win, 0,0,0,0, scr->scr_width - wPreferences.vedge_thickness - 1, event->xcrossing.y_root);
  1006.             wWorkspaceGetViewPosition(scr, scr->current_workspace, &x, &y);
  1007.             wWorkspaceSetViewPort(scr, scr->current_workspace, x+30, y);
  1008.         } else if (event->xcrossing.window == scr->virtual_edge_l) {
  1009.             XWarpPointer(dpy, None, scr->root_win, 0,0,0,0, wPreferences.vedge_thickness + 1, event->xcrossing.y_root);
  1010.             wWorkspaceGetViewPosition(scr, scr->current_workspace, &x, &y);
  1011.             wWorkspaceSetViewPort(scr, scr->current_workspace, x-30, y);
  1012.         } else if (event->xcrossing.window == scr->virtual_edge_u) {
  1013.             XWarpPointer(dpy, None, scr->root_win, 0,0,0,0, event->xcrossing.x_root, wPreferences.vedge_thickness + 1);
  1014.             wWorkspaceGetViewPosition(scr, scr->current_workspace, &x, &y);
  1015.             wWorkspaceSetViewPort(scr, scr->current_workspace, x, y-30);
  1016.         } else if (event->xcrossing.window == scr->virtual_edge_d) {
  1017.             XWarpPointer(dpy, None, scr->root_win, 0,0,0,0, event->xcrossing.x_root, scr->scr_height - wPreferences.vedge_thickness - 1);
  1018.             wWorkspaceGetViewPosition(scr, scr->current_workspace, &x, &y);
  1019.             wWorkspaceSetViewPort(scr, scr->current_workspace, x, y+30);
  1020.         }
  1021.     }
  1022. #endif
  1023.  
  1024.     if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify,
  1025.                    &ev)) {
  1026.     /* already left the window... */
  1027.     saveTimestamp(&ev);
  1028.     if (ev.xcrossing.mode==event->xcrossing.mode
  1029.         && ev.xcrossing.detail==event->xcrossing.detail) {
  1030.         return;
  1031.     }
  1032.     }
  1033.  
  1034.     if (XFindContext(dpy, event->xcrossing.window, wWinContext,
  1035.                      (XPointer *)&desc)!=XCNOENT) {
  1036.         if(desc->handle_enternotify)
  1037.             (*desc->handle_enternotify)(desc, event);
  1038.     }
  1039.  
  1040.     /* enter to window */
  1041.     wwin = wWindowFor(event->xcrossing.window);
  1042.     if (!wwin) {
  1043.     if (wPreferences.focus_mode==WKF_POINTER
  1044.         && event->xcrossing.window==event->xcrossing.root) {
  1045.         wSetFocusTo(scr, NULL);
  1046.     }
  1047.     if (wPreferences.colormap_mode==WKF_POINTER) {
  1048.         wColormapInstallForWindow(scr, NULL);
  1049.     }
  1050.     if (scr->autoRaiseTimer 
  1051.         && event->xcrossing.root==event->xcrossing.window) {
  1052.         WMDeleteTimerHandler(scr->autoRaiseTimer);
  1053.         scr->autoRaiseTimer = NULL;
  1054.     }
  1055.     } else {
  1056.     /* set auto raise timer even if in focus-follows-mouse mode
  1057.      * and the event is for the frame window, even if the window
  1058.      * has focus already.  useful if you move the pointer from a focused
  1059.      * window to the root window and back pretty fast 
  1060.      *
  1061.      * set focus if in focus-follows-mouse mode and the event
  1062.      * is for the frame window and window doesn't have focus yet */
  1063.     if ((wPreferences.focus_mode==WKF_POINTER
  1064.          || wPreferences.focus_mode==WKF_SLOPPY)
  1065.         && wwin->frame->core->window==event->xcrossing.window
  1066.         && !scr->flags.doing_alt_tab) {
  1067.         
  1068.         if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
  1069.         wSetFocusTo(scr, wwin);
  1070.         
  1071.         if (scr->autoRaiseTimer)
  1072.         WMDeleteTimerHandler(scr->autoRaiseTimer);
  1073.         scr->autoRaiseTimer = NULL;
  1074.         
  1075.         if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
  1076.         scr->autoRaiseWindow = wwin->frame->core->window;
  1077.         scr->autoRaiseTimer
  1078.             = WMAddTimerHandler(wPreferences.raise_delay,
  1079.                     (WMCallback*)raiseWindow, scr);
  1080.         }
  1081.     }
  1082.     /* Install colormap for window, if the colormap installation mode
  1083.      * is colormap_follows_mouse */
  1084.     if (wPreferences.colormap_mode==WKF_POINTER) {
  1085.         if (wwin->client_win==event->xcrossing.window)
  1086.         wColormapInstallForWindow(scr, wwin);
  1087.         else
  1088.         wColormapInstallForWindow(scr, NULL);
  1089.     }
  1090.     }
  1091.     
  1092.     /* a little kluge to hide the clip balloon */
  1093.     if (!wPreferences.flags.noclip && scr->flags.clip_balloon_mapped) {
  1094.     if (!desc) {
  1095.         XUnmapWindow(dpy, scr->clip_balloon);
  1096.         scr->flags.clip_balloon_mapped = 0;
  1097.     } else {
  1098.         if (desc->parent_type!=WCLASS_DOCK_ICON
  1099.         || scr->clip_icon != desc->parent) {
  1100.         XUnmapWindow(dpy, scr->clip_balloon);
  1101.         scr->flags.clip_balloon_mapped = 0;
  1102.         }
  1103.     }
  1104.     }
  1105.  
  1106.     if (event->xcrossing.window == event->xcrossing.root
  1107.     && event->xcrossing.detail == NotifyNormal
  1108.     && event->xcrossing.detail != NotifyInferior
  1109.     && wPreferences.focus_mode != WKF_CLICK) {
  1110.  
  1111.     wSetFocusTo(scr, scr->focused_window);
  1112.     }
  1113.  
  1114. #ifdef BALLOON_TEXT
  1115.     wBalloonEnteredObject(scr, desc);
  1116. #endif
  1117. }
  1118.  
  1119.  
  1120. static void
  1121. handleLeaveNotify(XEvent *event)
  1122. {
  1123.     WObjDescriptor *desc = NULL;
  1124.  
  1125.     if (XFindContext(dpy, event->xcrossing.window, wWinContext, 
  1126.              (XPointer *)&desc)!=XCNOENT) {
  1127.         if(desc->handle_leavenotify)
  1128.         (*desc->handle_leavenotify)(desc, event);
  1129.     }
  1130.     if (event->xcrossing.window == event->xcrossing.root
  1131.     && event->xcrossing.mode == NotifyNormal
  1132.     && event->xcrossing.detail != NotifyInferior
  1133.     && wPreferences.focus_mode != WKF_CLICK) {
  1134.  
  1135.     WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
  1136.  
  1137.     wSetFocusTo(scr, NULL);
  1138.     }
  1139. }
  1140.  
  1141.  
  1142. #ifdef SHAPE
  1143. static void
  1144. handleShapeNotify(XEvent *event)
  1145. {
  1146.     XShapeEvent *shev = (XShapeEvent*)event;
  1147.     WWindow *wwin;
  1148.     XEvent ev;
  1149. #ifdef DEBUG
  1150.     L("got shape notify");
  1151. #endif
  1152.     while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev)) {
  1153.     XShapeEvent *sev = (XShapeEvent*)&ev;
  1154.  
  1155.     if (sev->kind == ShapeBounding) {
  1156.         if (sev->shaped == shev->shaped) {
  1157.         *shev = *sev;
  1158.         } else {
  1159.         XPutBackEvent(dpy, &ev);
  1160.         break;
  1161.         }
  1162.     }
  1163.     }
  1164.  
  1165.     wwin = wWindowFor(shev->window);
  1166.     if (!wwin || shev->kind != ShapeBounding)
  1167.       return;
  1168.  
  1169.     if (!shev->shaped && wwin->flags.shaped) {
  1170.  
  1171.     wwin->flags.shaped = 0;
  1172.     wWindowClearShape(wwin);
  1173.  
  1174.     } else if (shev->shaped) {
  1175.  
  1176.     wwin->flags.shaped = 1;
  1177.     wWindowSetShape(wwin);
  1178.     }
  1179. }
  1180. #endif /* SHAPE */
  1181.  
  1182. #ifdef KEEP_XKB_LOCK_STATUS   
  1183. /* please help ]d if you know what to do */
  1184. handleXkbIndicatorStateNotify(XEvent *event)
  1185. {
  1186.     WWindow *wwin;
  1187.     WScreen *scr;
  1188.     XkbStateRec staterec;
  1189.     int i;
  1190.  
  1191.     for (i=0; i<wScreenCount; i++) {
  1192.         scr = wScreenWithNumber(i);
  1193.     wwin = scr->focused_window;
  1194.     if (wwin && wwin->flags.focused) {
  1195.         XkbGetState(dpy,XkbUseCoreKbd,&staterec);
  1196.         if (wwin->frame->languagemode != staterec.group) {
  1197.             wwin->frame->last_languagemode = wwin->frame->languagemode;
  1198.             wwin->frame->languagemode = staterec.group;
  1199.         }
  1200. #ifdef XKB_BUTTON_HINT
  1201.         if (wwin->frame->titlebar) {
  1202.             wFrameWindowPaint(wwin->frame);
  1203.         }
  1204. #endif
  1205.     }
  1206.     }
  1207. }
  1208. #endif /*KEEP_XKB_LOCK_STATUS*/
  1209.  
  1210. static void 
  1211. handleColormapNotify(XEvent *event)
  1212. {
  1213.     WWindow *wwin;
  1214.     WScreen *scr;
  1215.     Bool reinstall = False;
  1216.     
  1217.     wwin = wWindowFor(event->xcolormap.window);
  1218.     if (!wwin)
  1219.     return;
  1220.     
  1221.     scr = wwin->screen_ptr;
  1222.  
  1223.     do {
  1224.     if (wwin) {
  1225.         if (event->xcolormap.new) {
  1226.         XWindowAttributes attr;
  1227.         
  1228.         XGetWindowAttributes(dpy, wwin->client_win, &attr);
  1229.  
  1230.         if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
  1231.             scr->current_colormap = attr.colormap;
  1232.  
  1233.         reinstall = True;
  1234.         } else if (event->xcolormap.state == ColormapUninstalled &&
  1235.                scr->current_colormap == event->xcolormap.colormap) {
  1236.  
  1237.         /* some bastard app (like XV) removed our colormap */
  1238.         /* 
  1239.          * can't enforce or things like xscreensaver wont work
  1240.          * reinstall = True;
  1241.          */
  1242.         } else if (event->xcolormap.state == ColormapInstalled &&
  1243.                scr->current_colormap == event->xcolormap.colormap) {
  1244.  
  1245.         /* someone has put our colormap back */
  1246.         reinstall = False;
  1247.         }
  1248.     }
  1249.     } while (XCheckTypedEvent(dpy, ColormapNotify, event)
  1250.          && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
  1251.  
  1252.     if (reinstall && scr->current_colormap!=None) {
  1253.     if (!scr->flags.colormap_stuff_blocked)
  1254.         XInstallColormap(dpy, scr->current_colormap);
  1255.     }
  1256. }
  1257.  
  1258.  
  1259.  
  1260. static void
  1261. handleFocusIn(XEvent *event)
  1262. {
  1263.     WWindow *wwin;
  1264.  
  1265.     /*
  1266.      * For applications that like stealing the focus.
  1267.      */
  1268.     while (XCheckTypedEvent(dpy, FocusIn, event));    
  1269.     saveTimestamp(event);
  1270.     if (event->xfocus.mode == NotifyUngrab
  1271.     || event->xfocus.mode == NotifyGrab
  1272.     || event->xfocus.detail > NotifyNonlinearVirtual) {
  1273.     return;
  1274.     }
  1275.  
  1276.     wwin = wWindowFor(event->xfocus.window);
  1277.     if (wwin && !wwin->flags.focused) {
  1278.     if (wwin->flags.mapped)
  1279.         wSetFocusTo(wwin->screen_ptr, wwin);
  1280.     else
  1281.         wSetFocusTo(wwin->screen_ptr, NULL);
  1282.     } else if (!wwin) {
  1283.     WScreen *scr = wScreenForWindow(event->xfocus.window);
  1284.     if (scr)
  1285.         wSetFocusTo(scr, NULL);
  1286.     }
  1287. }
  1288.  
  1289.  
  1290. static WWindow*
  1291. windowUnderPointer(WScreen *scr)
  1292. {
  1293.     unsigned int mask;
  1294.     int foo;
  1295.     Window bar, win;
  1296.     
  1297.     if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo,
  1298.               &mask)) 
  1299.     return wWindowFor(win);
  1300.     return NULL;
  1301. }
  1302.  
  1303.  
  1304.  
  1305.  
  1306. static void
  1307. doWindozeCycle(WWindow *wwin, XEvent *event, Bool next)
  1308. {
  1309.     WScreen *scr = wScreenForRootWindow(event->xkey.root);
  1310.     Bool done = False;
  1311.     Bool openedSwitchMenu = False;
  1312.     WWindow *newFocused;
  1313.     WWindow *oldFocused;
  1314.     int modifiers;
  1315.     XModifierKeymap *keymap;
  1316.  
  1317.     if (!wwin)
  1318.     return;
  1319.  
  1320.     keymap = XGetModifierMapping(dpy);
  1321.  
  1322.     
  1323.     XGrabKeyboard(dpy, scr->root_win, False, GrabModeAsync, GrabModeAsync, 
  1324.           CurrentTime);
  1325.  
  1326.     if (next) {
  1327.     newFocused = NextToFocusAfter(wwin);
  1328.     } else {
  1329.     newFocused = NextToFocusBefore(wwin); 
  1330.     }
  1331.  
  1332.     scr->flags.doing_alt_tab = 1;
  1333.  
  1334.  
  1335.     if (wPreferences.circ_raise)
  1336.     XRaiseWindow(dpy, newFocused->frame->core->window);
  1337.     wWindowFocus(newFocused, scr->focused_window);
  1338.     oldFocused = newFocused;
  1339.  
  1340. #if 0
  1341.     if (wPreferences.popup_switchmenu && 
  1342.     (!scr->switch_menu || !scr->switch_menu->flags.mapped)) {
  1343.  
  1344.     OpenSwitchMenu(scr, scr->scr_width/2, scr->scr_height/2, False);
  1345.     openedSwitchMenu = True;
  1346.     }
  1347. #endif    
  1348.     while (!done) {
  1349.     XEvent ev;
  1350.  
  1351.     WMMaskEvent(dpy,KeyPressMask|KeyReleaseMask|ExposureMask, &ev);
  1352.  
  1353.     if (ev.type != KeyRelease && ev.type != KeyPress) {
  1354.         WMHandleEvent(&ev);
  1355.         continue;
  1356.     }
  1357.     /* ignore CapsLock */
  1358.     modifiers = ev.xkey.state & ValidModMask;
  1359.  
  1360.     if (ev.type == KeyPress) {
  1361.         if (wKeyBindings[WKBD_FOCUSNEXT].keycode == ev.xkey.keycode
  1362.         && wKeyBindings[WKBD_FOCUSNEXT].modifier == modifiers) {
  1363.  
  1364.         UpdateSwitchMenu(scr, newFocused, ACTION_CHANGE_STATE);
  1365.         newFocused = NextToFocusAfter(newFocused);
  1366.         wWindowFocus(newFocused, oldFocused);
  1367.         oldFocused = newFocused;
  1368.         
  1369.         if (wPreferences.circ_raise) {
  1370.             /* restore order */
  1371.             CommitStacking(scr);
  1372.             XRaiseWindow(dpy, newFocused->frame->core->window);
  1373.         }
  1374.  
  1375.         UpdateSwitchMenu(scr, newFocused, ACTION_CHANGE_STATE);
  1376.  
  1377.         } else if (wKeyBindings[WKBD_FOCUSPREV].keycode == ev.xkey.keycode
  1378.                && wKeyBindings[WKBD_FOCUSPREV].modifier == modifiers) {
  1379.  
  1380.         UpdateSwitchMenu(scr, newFocused, ACTION_CHANGE_STATE);
  1381.         newFocused = NextToFocusBefore(newFocused);
  1382.         wWindowFocus(newFocused, oldFocused);
  1383.         oldFocused = newFocused;
  1384.  
  1385.         if (wPreferences.circ_raise) {
  1386.             /* restore order */
  1387.             CommitStacking(scr);
  1388.             XRaiseWindow(dpy, newFocused->frame->core->window);
  1389.         }
  1390.         UpdateSwitchMenu(scr, newFocused, ACTION_CHANGE_STATE);
  1391.  
  1392.         } else {
  1393.         handleKeyPress(&ev);
  1394.         }
  1395.         
  1396.         /*
  1397.         else if (wKeyBindings[WKBD_LOWER].keycode == ev.xkey.keycode
  1398.                && wKeyBindings[WKBD_LOWER].modifier == modifiers) {
  1399.  
  1400.         wLowerFrame(newFocused->frame->core);
  1401.  
  1402.         } else if (wKeyBindings[WKBD_RAISE].keycode == ev.xkey.keycode
  1403.                && wKeyBindings[WKBD_RAISE].modifier == modifiers) {
  1404.  
  1405.         wRaiseFrame(newFocused->frame->core);
  1406.         }*/
  1407.     } else if (ev.type == KeyRelease) {
  1408.         int i;
  1409.  
  1410.         for (i = 0; i < 8 * keymap->max_keypermod; i++) {
  1411.         if (keymap->modifiermap[i] == ev.xkey.keycode &&
  1412.             wKeyBindings[WKBD_FOCUSNEXT].modifier 
  1413.             & 1<<(i/keymap->max_keypermod)) {
  1414.             done = True;
  1415.             break;
  1416.         }
  1417.         }
  1418.     }
  1419.     }
  1420.     XFreeModifiermap(keymap);
  1421.  
  1422.     XUngrabKeyboard(dpy, CurrentTime);
  1423.     wSetFocusTo(scr, newFocused);
  1424.  
  1425.     if (wPreferences.circ_raise) {
  1426.     wRaiseFrame(newFocused->frame->core);
  1427.         CommitStacking(scr);
  1428.     }
  1429.  
  1430.     scr->flags.doing_alt_tab = 0;
  1431.     if (openedSwitchMenu) 
  1432.     OpenSwitchMenu(scr, scr->scr_width/2, scr->scr_height/2, False);
  1433. }
  1434.  
  1435.  
  1436.  
  1437.  
  1438. static void
  1439. handleKeyPress(XEvent *event)
  1440. {
  1441.     WScreen *scr = wScreenForRootWindow(event->xkey.root);
  1442.     WWindow *wwin = scr->focused_window;
  1443.     int i;
  1444.     int modifiers;
  1445.     int command=-1, index;
  1446. #ifdef KEEP_XKB_LOCK_STATUS   
  1447.     XkbStateRec staterec;     
  1448. #endif /*KEEP_XKB_LOCK_STATUS*/
  1449.  
  1450.     /* ignore CapsLock */
  1451.     modifiers = event->xkey.state & ValidModMask;
  1452.  
  1453.     for (i=0; i<WKBD_LAST; i++) {
  1454.     if (wKeyBindings[i].keycode==0) 
  1455.         continue;
  1456.     
  1457.     if (wKeyBindings[i].keycode==event->xkey.keycode
  1458.         && (/*wKeyBindings[i].modifier==0 
  1459.         ||*/ wKeyBindings[i].modifier==modifiers)) {
  1460.         command = i;
  1461.         break;
  1462.     }
  1463.     }
  1464.         
  1465.     
  1466.     if (command < 0) {
  1467. #ifdef LITE
  1468.     { 
  1469. #if 0
  1470.     }
  1471. #endif
  1472. #else
  1473.     if (!wRootMenuPerformShortcut(event)) {
  1474. #endif
  1475.         static int dontLoop = 0;
  1476.         
  1477.         if (dontLoop > 10) {
  1478.         wwarning("problem with key event processing code");
  1479.         return;
  1480.         }
  1481.         dontLoop++;
  1482.         /* if the focused window is an internal window, try redispatching
  1483.          * the event to the managed window, as it can be a WINGs window */
  1484.         if (wwin && wwin->flags.internal_window
  1485.         && wwin->client_leader!=None) {
  1486.         /* client_leader contains the WINGs toplevel */
  1487.         event->xany.window = wwin->client_leader;
  1488.         WMHandleEvent(event);
  1489.         }
  1490.         dontLoop--;
  1491.     }
  1492.     return;
  1493.     }
  1494.  
  1495. #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
  1496. #define ISFOCUSED(w) ((w) && (w)->flags.focused)
  1497.  
  1498.     switch (command) {
  1499. #ifndef LITE
  1500.      case WKBD_ROOTMENU:
  1501.     OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True);
  1502.     break;
  1503.      case WKBD_WINDOWLIST:
  1504.     OpenSwitchMenu(scr, event->xkey.x_root, event->xkey.y_root, True);
  1505.     break;
  1506. #endif /* !LITE */
  1507.      case WKBD_WINDOWMENU:
  1508.     if (ISMAPPED(wwin) && ISFOCUSED(wwin))
  1509.         OpenWindowMenu(wwin, wwin->frame_x, 
  1510.                wwin->frame_y+wwin->frame->top_width, True);
  1511.     break;
  1512.      case WKBD_MINIATURIZE:
  1513.     if (ISMAPPED(wwin) && ISFOCUSED(wwin) 
  1514.         && !WFLAGP(wwin, no_miniaturizable)) {
  1515.         CloseWindowMenu(scr);
  1516.         
  1517.         if (wwin->protocols.MINIATURIZE_WINDOW)
  1518.               wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
  1519.                     event->xbutton.time);
  1520.         else {
  1521.         wIconifyWindow(wwin);
  1522.         }
  1523.     }
  1524.     break;
  1525.      case WKBD_HIDE:
  1526.     if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
  1527.         WApplication *wapp = wApplicationOf(wwin->main_window);
  1528.         CloseWindowMenu(scr);
  1529.         
  1530.         if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
  1531.         wHideApplication(wapp);
  1532.         }
  1533.     }
  1534.     break;
  1535.      case WKBD_MAXIMIZE:
  1536.     if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_resizable)) {
  1537.         CloseWindowMenu(scr);
  1538.         
  1539.         if (wwin->flags.maximized) {
  1540.         wUnmaximizeWindow(wwin);
  1541.         } else {
  1542.         wMaximizeWindow(wwin, MAX_VERTICAL|MAX_HORIZONTAL);
  1543.         }
  1544.     }
  1545.     break;
  1546.      case WKBD_VMAXIMIZE:
  1547.     if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_resizable)) {
  1548.         CloseWindowMenu(scr);
  1549.         
  1550.         if (wwin->flags.maximized) {
  1551.         wUnmaximizeWindow(wwin);
  1552.         } else {
  1553.         wMaximizeWindow(wwin, MAX_VERTICAL);
  1554.         }
  1555.     }
  1556.     break;
  1557.      case WKBD_RAISE:
  1558.     if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
  1559.         CloseWindowMenu(scr);
  1560.         
  1561.         wRaiseFrame(wwin->frame->core);
  1562.     }
  1563.     break;
  1564.      case WKBD_LOWER:
  1565.     if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
  1566.         CloseWindowMenu(scr);
  1567.  
  1568.         wLowerFrame(wwin->frame->core);
  1569.     }
  1570.     break;
  1571.      case WKBD_RAISELOWER:
  1572.     /* raise or lower the window under the pointer, not the
  1573.      * focused one 
  1574.      */
  1575.     wwin = windowUnderPointer(scr);
  1576.     if (wwin)
  1577.         wRaiseLowerFrame(wwin->frame->core);
  1578.     break;
  1579.      case WKBD_SHADE:
  1580.     if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
  1581.         if (wwin->flags.shaded)
  1582.         wUnshadeWindow(wwin);
  1583.         else
  1584.         wShadeWindow(wwin);
  1585.     }
  1586.     break;
  1587.      case WKBD_MOVERESIZE:
  1588.     if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
  1589.         CloseWindowMenu(scr);
  1590.         
  1591.         wKeyboardMoveResizeWindow(wwin);
  1592.     }
  1593.     break;
  1594.      case WKBD_CLOSE:
  1595.     if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
  1596.         CloseWindowMenu(scr);
  1597.         if (wwin->protocols.DELETE_WINDOW)
  1598.           wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW,
  1599.                   event->xkey.time);
  1600.     }
  1601.     break;
  1602.     case WKBD_SELECT:
  1603.     if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
  1604.         wSelectWindow(wwin, !wwin->flags.selected);
  1605.     }
  1606.         break;
  1607.      case WKBD_FOCUSNEXT:
  1608.     if (wKeyBindings[WKBD_FOCUSNEXT].modifier != 0)
  1609.         doWindozeCycle(wwin, event, True);
  1610.     else
  1611.         puts("NEXT");
  1612.     break;
  1613.  
  1614.      case WKBD_FOCUSPREV:
  1615.     if (wKeyBindings[WKBD_FOCUSPREV].modifier != 0)
  1616.         doWindozeCycle(wwin, event, False);
  1617.     else
  1618.         puts("PREV");
  1619.     break;
  1620.  
  1621. #if (defined(__STDC__) && !defined(UNIXCPP)) || defined(ANSICPP)
  1622. #define GOTOWORKS(wk)    case WKBD_WORKSPACE##wk:\
  1623.             i = (scr->current_workspace/10)*10 + wk - 1;\
  1624.             if (wPreferences.ws_advance || i<scr->workspace_count)\
  1625.                 wWorkspaceChange(scr, i);\
  1626.             break
  1627. #else
  1628. #define GOTOWORKS(wk)    case WKBD_WORKSPACE/**/wk:\
  1629.             i = (scr->current_workspace/10)*10 + wk - 1;\
  1630.             if (wPreferences.ws_advance || i<scr->workspace_count)\
  1631.                 wWorkspaceChange(scr, i);\
  1632.             break
  1633. #endif
  1634.     GOTOWORKS(1);
  1635.     GOTOWORKS(2);
  1636.     GOTOWORKS(3);
  1637.     GOTOWORKS(4);
  1638.     GOTOWORKS(5);
  1639.     GOTOWORKS(6);
  1640.     GOTOWORKS(7);
  1641.     GOTOWORKS(8);
  1642.     GOTOWORKS(9);
  1643.     GOTOWORKS(10);
  1644. #undef GOTOWORKS
  1645.      case WKBD_NEXTWORKSPACE:
  1646.     wWorkspaceRelativeChange(scr, 1);
  1647.     break;
  1648.      case WKBD_PREVWORKSPACE:
  1649.     wWorkspaceRelativeChange(scr, -1);
  1650.     break;
  1651.      case WKBD_WINDOW1:
  1652.      case WKBD_WINDOW2:
  1653.      case WKBD_WINDOW3:
  1654.      case WKBD_WINDOW4:
  1655. #ifdef EXTEND_WINDOWSHORTCUT
  1656.      case WKBD_WINDOW5:
  1657.      case WKBD_WINDOW6:
  1658.      case WKBD_WINDOW7:
  1659.      case WKBD_WINDOW8:
  1660.      case WKBD_WINDOW9:
  1661.      case WKBD_WINDOW10:
  1662. #endif
  1663.  
  1664. #define INITBAG(bag)  if (bag) WMEmptyBag(bag); else bag = WMCreateBag(4)
  1665.  
  1666.     index = command-WKBD_WINDOW1;
  1667.     
  1668.         if (scr->shortcutWindows[index]) {
  1669.             WMBag *list = scr->shortcutWindows[index];
  1670.             int cw;
  1671.         int count = WMGetBagItemCount(list);
  1672.         WWindow *twin;
  1673.         WMBagIterator iter;
  1674.         WWindow *wwin;
  1675.  
  1676.             wUnselectWindows(scr);
  1677.             cw = scr->current_workspace;
  1678.  
  1679.         for (wwin = WMBagLast(list, &iter);
  1680.          iter != NULL;
  1681.          wwin = WMBagPrevious(list, &iter)) {
  1682.             
  1683.         if (count > 1)
  1684.             wWindowChangeWorkspace(wwin, cw);
  1685.  
  1686.                 wMakeWindowVisible(wwin);
  1687.  
  1688.         if (count > 1)
  1689.             wSelectWindow(wwin, True);
  1690.             }
  1691.         
  1692.         /* rotate the order of windows, to create a cycling effect */
  1693.         twin = WMBagFirst(list, &iter);
  1694.         WMRemoveFromBag(list, twin);
  1695.         WMPutInBag(list, twin);
  1696.  
  1697.         } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
  1698.  
  1699.         INITBAG(scr->shortcutWindows[index]);
  1700.             WMPutInBag(scr->shortcutWindows[index], wwin);
  1701.  
  1702.             if (wwin->flags.selected && scr->selected_windows) {
  1703.                 WMBag *selwins = scr->selected_windows;
  1704.         int i;
  1705.  
  1706.         for (i = 0; i < WMGetBagItemCount(selwins); i++) {
  1707.             WWindow *tmp = WMGetFromBag(selwins, i);
  1708.  
  1709.             if (tmp != wwin)
  1710.             WMPutInBag(scr->shortcutWindows[index], tmp);
  1711.                 }
  1712.             }
  1713.             wSelectWindow(wwin, !wwin->flags.selected);
  1714.             XFlush(dpy);
  1715.             wusleep(3000);
  1716.             wSelectWindow(wwin, !wwin->flags.selected);
  1717.             XFlush(dpy);
  1718.  
  1719.         } else if (scr->selected_windows 
  1720.            && WMGetBagItemCount(scr->selected_windows)) {
  1721.  
  1722.             if (wwin->flags.selected && scr->selected_windows) {
  1723.                 WMBag *selwins = scr->selected_windows;
  1724.         int i;
  1725.  
  1726.                 INITBAG(scr->shortcutWindows[index]);
  1727.  
  1728.         for (i = 0; i < WMGetBagItemCount(selwins); i++) {
  1729.             WWindow *tmp = WMGetFromBag(selwins, i);
  1730.  
  1731.                     WMPutInBag(scr->shortcutWindows[index],  tmp);
  1732.                 }
  1733.             }
  1734.         }
  1735. #undef INITBAG
  1736.  
  1737.         break;
  1738.      case WKBD_NEXTWSLAYER:
  1739.      case WKBD_PREVWSLAYER:
  1740.     {
  1741.         int row, column;
  1742.         
  1743.         row = scr->current_workspace/10;
  1744.         column = scr->current_workspace%10;
  1745.         
  1746.         if (command==WKBD_NEXTWSLAYER) {
  1747.         if ((row+1)*10 < scr->workspace_count)
  1748.             wWorkspaceChange(scr, column+(row+1)*10);
  1749.         } else {
  1750.         if (row > 0)
  1751.             wWorkspaceChange(scr, column+(row-1)*10);
  1752.         }
  1753.     }
  1754.     break;
  1755.     case WKBD_CLIPLOWER:
  1756.         if (!wPreferences.flags.noclip)
  1757.             wDockLower(scr->workspaces[scr->current_workspace]->clip);
  1758.         break;
  1759.      case WKBD_CLIPRAISE:
  1760.         if (!wPreferences.flags.noclip)
  1761.             wDockRaise(scr->workspaces[scr->current_workspace]->clip);
  1762.         break;
  1763.      case WKBD_CLIPRAISELOWER:
  1764.         if (!wPreferences.flags.noclip)
  1765.             wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
  1766.         break;
  1767. #ifdef KEEP_XKB_LOCK_STATUS
  1768.      case WKBD_TOGGLE:
  1769.         if(wPreferences.modelock) {
  1770.             /*toggle*/
  1771.             wwin = scr->focused_window;
  1772.  
  1773.             if (wwin && wwin->flags.mapped
  1774.                 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
  1775.                 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
  1776.                 XkbGetState(dpy,XkbUseCoreKbd,&staterec);
  1777.  
  1778.                 wwin->frame->languagemode = wwin->frame->last_languagemode;
  1779.                 wwin->frame->last_languagemode = staterec.group;
  1780.                 XkbLockGroup(dpy,XkbUseCoreKbd, wwin->frame->languagemode);
  1781.                                  
  1782.             }
  1783.         }
  1784.         break;
  1785. #endif /* KEEP_XKB_LOCK_STATUS */
  1786.  
  1787.     }
  1788. }
  1789.  
  1790.  
  1791. static void 
  1792. handleMotionNotify(XEvent *event)
  1793. {
  1794.     WMenu *menu;
  1795.     WScreen *scr = wScreenForRootWindow(event->xmotion.root);
  1796.  
  1797.     if (wPreferences.scrollable_menus) {
  1798.         if (scr->flags.jump_back_pending ||
  1799.             event->xmotion.x_root <= 1 ||
  1800.             event->xmotion.x_root >= (scr->scr_width - 2) ||
  1801.             event->xmotion.y_root <= 1 ||
  1802.             event->xmotion.y_root >= (scr->scr_height - 2)) {
  1803. #ifdef DEBUG
  1804.             L("pointer at screen edge");
  1805. #endif
  1806.             menu = wMenuUnderPointer(scr);
  1807.             if (menu!=NULL)
  1808.                 wMenuScroll(menu, event);
  1809.         }
  1810.     }
  1811. #if 0
  1812.     if (event->xmotion.subwindow == None)
  1813.     return;
  1814.  
  1815.     if (scr->scrolledFMaximize != None) {
  1816.         WWindow *twin;
  1817.  
  1818.         twin = wWindowFor(scr->scrolledFMaximize);
  1819.         if (twin && twin->frame_y ==) {
  1820.         
  1821.         
  1822.         }
  1823.         scr->scrolledFMaximize = NULL;
  1824.         
  1825.     } else {
  1826.  
  1827.     /* scroll full maximized window */
  1828.     if (event->xmotion.y_root < 1
  1829.     || event->xmotion.y_root > scr->scr_height - 1) {
  1830.  
  1831.     wwin = wWindowFor(event->xmotion.subwindow);
  1832.  
  1833.     if (wwin && (wwin->flags.maximized & MAX_VERTICAL)
  1834.         && WFLAGP(wwin, full_maximize) 
  1835.         && event->xmotion.x_root >= wwin->frame_x
  1836.         && event->xmotion.x_root <= wwin->frame_x + wwin->frame->core->width) {
  1837.  
  1838.         if (!WFLAGP(wwin, no_titlebar)
  1839.         && wwin->frame_y <= - wwin->frame->top_width) {
  1840.  
  1841.         wWindowMove(wwin, wwin->frame_x, 0);
  1842.         wwin->flags.dragged_while_fmaximized = 0;
  1843.  
  1844.         } else if (!WFLAGP(wwin, no_resizebar)
  1845.                && wwin->frame_y + wwin->frame->core->height >=
  1846.                scr->scr_height + wwin->frame->bottom_width) {
  1847.         
  1848.         int y = scr->scr_height + wwin->frame->bottom_width;
  1849.  
  1850.         y = scr->scr_height - wwin->frame_y - wwin->frame->core->height;
  1851.  
  1852.         wWindowMove(wwin, wwin->frame_x, y);
  1853.         wwin->flags.dragged_while_fmaximized = 0;
  1854.         }
  1855.     }
  1856.     }
  1857. #endif
  1858. }
  1859.  
  1860.  
  1861. static void
  1862. handleVisibilityNotify(XEvent *event)
  1863. {
  1864.     WWindow *wwin;
  1865.  
  1866.     wwin = wWindowFor(event->xvisibility.window);
  1867.     if (!wwin)
  1868.     return;
  1869.     wwin->flags.obscured = 
  1870.     (event->xvisibility.state == VisibilityFullyObscured);
  1871. }
  1872.